# encoding:utf-8
''' 2017-8-24 pylint-fix '''
import os
import sys
from optparse import OptionParser
from _helpres import BatchPackimg

class PackSheet(object):

    @classmethod 
    def _svn_commit(cls):
        #父级路径添加
        cmd_str0 = "cd {} && svn add *".format(cls._tarpath)
        os.system(cmd_str0)
        #子路径遍历添加
        def _iter_addfile(tarpath):
            for filename in os.listdir(tarpath):
                fileobj = os.path.join(tarpath, filename)
                if os.path.isdir(fileobj):
                    cmd_str = "svn add {}/*".format(fileobj)
                else:
                    cmd_str = "svn add {}".format(fileobj)
                os.system(cmd_str)
        _iter_addfile(cls._tarpath)     
        #提交
        cmd_str2 = '''cd {} && svn ci -m "AUTO:Jenkins tools commit"  --username duanwenbin --password E6VFbB5o 
'''.format(cls._tarpath)
        os.system(cmd_str2)

    @classmethod
    def _svn_update(cls):
        cmd_str2 = "cd {} && svn update --username duanwenbin --password E6VFbB5o ".format(cls._basepath)
        os.system(cmd_str2)


    @classmethod 
    def _git_commit(cls):
        pass

    ''' 打包文件 '''
    @classmethod
    def execute(cls, basepath, tarpath, conflist, iscommit=True, packtype="png"):
        ''' execute '''
        cls._tarpath = tarpath
        cls._basepath = basepath
        cls._svn_update()
        BatchPackimg.excute(basepath, tarpath, conflist, packtype)
        if iscommit:
            cls._svn_commit()
    
        

if __name__ == "__main__":
    print("======================================")
    print("=============SheetPack================")
    print("======================================")

    DEBUG = False
    if DEBUG is True:
        basepath = "/data0/public_work/yuenan/share/resource/"
        tarpath = "/data0/public_work/yuenan/client/trunk/res"

        #test cmds 
        # jfilepath = "G:\\public_redwar\\share\\RaRes\\res_source\\packcfg.json"
        # jfile = open(jfilepath, "r")
        # import json
        # pcfg = json.load(jfile)
        # for key, cmd in pcfg.items():
        #     if not key.startswith("NO"):
        #         PackSheet.execute(basepath, tarpath, cmd, True)



        PackSheet.execute(basepath, tarpath, '''common_1''', iscommit = False)
        sys.exit()

    parser = OptionParser()
    # type 1:public apk by a jsonfile
    parser.add_option("-b", "--base", dest="basepath",
                      default=None, help="Input the base path!")
    parser.add_option("-t", "--tarpath", dest="tarpath", default=None,
                      help="Input the json config file for pack the apk!")
    parser.add_option("-l", "--conflist", dest="conflist",
                      default=None, help="Pack conf-list")
    
    (opts, args) = parser.parse_args()
    conflist = opts.conflist.split(",") 
    for conf in conflist:
        PackSheet.execute(opts.basepath, opts.tarpath, conf, True)

